// pages/user/account/addCard/cardCode.js

const app = getApp();
let api = require('../../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

    sendTime: '重新获取',
    sendColor: '#363636',
    snsMsgWait: 60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      data: options
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  //  onShareAppMessage: function() {

//  },

  // 输入监听
  inputWacth: function (e) {
   // console.log(e);
    let item = e.currentTarget.dataset.field;
    this.setData({
      [item]: e.detail.value
    });
  },

  /**
   * 提交
   */
  sumbit: function() {
    var that = this;
    var param = {
      "cardNo": that.data.data.cardno,
      "cardVest": that.data.data.name,
      verifyCode:that.data.code
    };
    var token = wx.getStorageSync('TOKEN');
    api._post('/wallet/bank/add', param, token).then(res => {
      if (res.code == 200) {

        wx.navigateBack({
          delta: 3
        })

      } 
      else if (res.code == 40000003 || res.code == 40000006 ){
        wx.showToast({
          title: res.message,
          icon: 'none',
        })
      }
      else if (res.code > 400) {
        wx.navigateTo({
          url: '../login/login',
        })
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none',
        })
      }
    })
    // wx.redirectTo({
    //   url: '../cardList/cardList',
    // })
  },
  getCode:function() {
    var that=this
    api._get('/sms/send/'+that.data.data.phone+'/1', '', '').then(res => {
     // console.log(res)
      if(res.code==200){
       
      }else{
        wx.showToast({
          title: '请稍后重试',
          icon: 'none',
        })
      }
    })
    
    var inter = setInterval(function () {
      that.setData({
        smsFlag: true,
        sendColor: '#cccccc',
        sendTime: that.data.snsMsgWait + 's后重发',
        snsMsgWait: that.data.snsMsgWait - 1
      });
      if (that.data.snsMsgWait < 0) {
        clearInterval(inter)
        that.setData({
          sendColor: '#ff8000',
          sendTime: '重新获取',
          snsMsgWait: 60,
          smsFlag: false
        });
      }
    }.bind(that), 1000);
  }
})